package com.md24housecall.fibromyalgiadiagnostic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import android.app.Activity;
import android.os.AsyncTask;
 
public class LoadingTask extends AsyncTask<String, Integer, Integer> {
 
    public interface LoadingTaskFinishedListener {
        void onTaskFinished(); // If you want to pass something back to the listener add a param to this method
    }
 
    // This is the progress bar you want to update while the task is in progress
    // private final ProgressBar progressBar;
    // This is the listener that will be told when this task is finished
    private final LoadingTaskFinishedListener finishedListener;
    final static DefaultHttpClient httpClient = new DefaultHttpClient();
    private Activity activity;
    
    public LoadingTask(LoadingTaskFinishedListener finishedListener, Activity activity) {
        //this.progressBar = progressBar;
        this.finishedListener = finishedListener;
        this.activity = activity;
    }
 
    @Override
    protected Integer doInBackground(String... urls) {
    	try {
    		
        	String url = activity.getResources().getString(R.string.url);
            HttpPost httpPost = new HttpPost(url);
           
            List<NameValuePair> nameValuePairs = getPostData(urls[0]);
            
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 3000);
            HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000);
            
            httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            //HttpEntity httpEntity;
            if(statusCode < 400){
            	//httpEntity = httpResponse.getEntity();
            	//String responseBody = EntityUtils.toString(httpEntity);
            }
           
        } catch (ConnectTimeoutException e){
        } catch (IOException e) {
        }
    	return 0;
    }
 
 
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        //progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar ( a UI view ) here
    }
 
    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        finishedListener.onTaskFinished(); // Tell whoever was listening we have finished
    }
    
    protected List<NameValuePair> getPostData(String partUrl){
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		String[] splits = partUrl.split("/");
		
		for(int i = 0; i < splits.length; i +=2){
			if(i == 0)
				nameValuePairs.add(new BasicNameValuePair("action", splits[i]));
			else
				nameValuePairs.add(new BasicNameValuePair(splits[i-1], splits[i]));
		}

	    return nameValuePairs;
	}
}